package com.example.demo0

import com.example.util.p


data class ApplyEvent(val money:Int,val title:String)

interface ApplyHandler{
    val successor:ApplyHandler?
    fun handleEvent(event: ApplyEvent)
}

class GroupLeader(override val successor: ApplyHandler?):ApplyHandler{
    override fun handleEvent(event: ApplyEvent) {
        when{
            event.money <= 100 -> p("Group Leader handled application:${event.title}")
            else -> when(successor){
                is ApplyHandler -> successor.handleEvent(event)
                else -> p("Group Leader:This application cannot be handle.")
            }
        }
    }

}


class President(override val successor: ApplyHandler?) :ApplyHandler{
    override fun handleEvent(event: ApplyEvent) {
        when{
            event.money <= 500 -> p("President handled application:${event.title}")
            else -> when(successor){
                is ApplyHandler -> successor.handleEvent(event)
                else -> p("President:This application cannot be handle")
            }
        }
    }


}


class College(override val successor: ApplyHandler?) :ApplyHandler{
    override fun handleEvent(event: ApplyEvent) {
        when{
            event.money > 1000 -> p("College:This application is refused")
            else -> p("College handled application:${event.title}")
        }
    }

}



fun main(){
    val college = College(null)
    val president = President(college)
    val groupLeader = GroupLeader(president)


    groupLeader.handleEvent(ApplyEvent(10,"buy a pen"))
    groupLeader.handleEvent(ApplyEvent(200,"team building"))
    groupLeader.handleEvent(ApplyEvent(600,"hold a debate match"))
    groupLeader.handleEvent(ApplyEvent(1200,"annual meeting of the college"))




}









